package com.lemzdo.community.controller;

import com.lemzdo.community.dto.NoticeDTO;
import com.lemzdo.community.enums.NoticeTypeEnum;
import com.lemzdo.community.model.User;
import com.lemzdo.community.service.NoticeService;
import com.lemzdo.community.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller//notice控制层 用于点击对应的通知,变为已读并且跳转到该通知对应的帖子页面
public class NoticeController {

    @Autowired
    private NoticeService noticeService;
    @Autowired
    private PostService postService;

    @RequestMapping(value = "/notice/{id}")
    public String notice(@PathVariable Integer id, HttpServletRequest request){

        //判断是否登录
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/";
        }

        //读取对应id的通知到主页面,并且转化状态，未读变成已读
        //传参数user 避免非法用户访问到了别人的消息
        NoticeDTO noticeDTO = noticeService.readNotice(id,user);


        if(NoticeTypeEnum.REPLY_POST.getMessage().equals(noticeDTO.getNoticeTypeMsg())
                ||NoticeTypeEnum.REPLY_COMMENT.getMessage().equals(noticeDTO.getNoticeTypeMsg())){
            //当通知是回复帖子或者回复评论  都跳转到帖子页面

            return "redirect:/post/" + noticeDTO.getPostId();
        }else {
            //其他功能 先返回主页面 todo.....
            return "redirect:/";
        }



    }


}
